<template>

  <div class="app-container">
    <!--    条件栏-->
    <el-form :model="queryParams" ref="queryForm" :inline="true" :rules="queryForm" v-show="showSearch" label-width="68px">
      <el-form-item label="资产类型" prop="assetsName">
        <el-input placeholder="请输入资产名称" v-model="queryParams.assetsName"  maxlength="30"
                  show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="资产类型" prop="assetsType">
        <el-select
          v-model="queryParams.assetsType"
          placeholder="资产类型"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="info" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--    操作栏-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['server:assets:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['server:assets:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['server:assets:export']"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-share"
          size="mini"
          @click="handleRemoteDevice"
          v-hasPermi="['server:assets:export']"
        >链接远程设备信息
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <!--    资产列表-->
    <el-table v-loading="loading" :data="assetsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" type="index"/>
      <el-table-column label="资产编号" align="center" key="assetsId" prop="assetsId" v-if="columns[1].visible"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="资产名称" align="center" key="assetsName" prop="assetsName"
                       v-if="columns[8].visible"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="资产类型" align="center" key="assetsType" prop="assetsType" :formatter="typeFormat"
                       v-if="columns[3].visible"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['server:assets:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['server:assets:remove']"
          >删除
          </el-button>
          <el-button
            align="center"
            size="mini"
            type="text"
            @click="handleSelect(scope.row)"
            v-hasPermi="['server:assets:query']"
          >查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--    分页栏-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资产记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" :inline="true">
        <el-form-item label="资产编号" prop="assetsId">
          <el-input v-model="form.assetsId" placeholder="请输入资产Id" class="form_row" maxlength="30"/>
        </el-form-item>
        <el-form-item label="资产Ip" prop="assetsIp">
          <el-input v-model="form.assetsIp" placeholder="请输入资产Ip" class="form_row" maxlength="30"/>
        </el-form-item>
        <el-form-item label="资产类型" prop="assetsType">
          <el-select v-model="form.assetsType" placeholder="请选择" class="form_row">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资产端口" prop="assetsPort">
          <el-input v-model="form.assetsPort" placeholder="请输入资产端口" class="form_row" maxlength="30"/>
        </el-form-item>
        <el-form-item label="资产账号" prop="assetsAccount">
          <el-input v-model="form.assetsAccount" placeholder="请输入资产账号" class="form_row" maxlength="30"/>
        </el-form-item>
        <el-form-item label="资产名称" prop="assetsName">
          <el-input v-model="form.assetsName" placeholder="请输入资产名称" class="form_row" maxlength="30"/>
        </el-form-item>
        <el-form-item label="采购时间" prop="purchaseTime">
          <el-date-picker
            class="form_row"
            v-model="form.purchaseTime"
            size="small"
            value-format="yyyy-MM-dd"
            type="date"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="维护周期" prop="maintenanceCycle">
          <el-select v-model="form.maintenanceCycle" placeholder="请选择" class="form_row">
            <el-option
              v-for="dict in cycleOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="资产状态" prop="assetsStatus">
          <el-select v-model="form.assetsStatus" placeholder="请选择" class="form_row">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark" style="display: block;">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" maxlength="200" style="width: 610px;"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看资产管理 -->
    <el-dialog :title="title" :visible.sync="look" width="800px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="资产编号：" prop="assetsId">
              {{form.assetsId}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资产Ip：" prop="assetsIp">
              {{form.assetsIp}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资产类型：" prop="assetsType">
              {{selectDictLabel(typeOptions, form.assetsType)}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资产端口：" prop="assetsPort">
              {{form.assetsPort}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资产账号：" prop="assetsAccount">
              {{form.assetsAccount}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资产名称：" prop="assetsName">
              {{form.assetsName}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="采购时间：">
              {{form.purchaseTime}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="维护周期：">
              {{selectDictLabel(cycleOptions, form.maintenanceCycle)}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资产状态：">
              {{selectDictLabel(statusOptions, form.assetsStatus)}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注：" prop="remark">
              {{form.remark}}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>


  import {
    addAssets,
    delAssets,
    exportAssets,
    getAssets,
    listAssets,
    updateAssets,
    queryAllMonitorInfos
  } from "../../../api/server/estates/assets";

  export default {
    name: 'assets',
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 资产表格数据
        assetsList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        //是否显示详细弹出层
        look: false,
        // 状态字典
        statusOptions: [],
        //类型字典
        typeOptions: [],
        //周期字典
        cycleOptions: [],
        dateRange: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          assetsType: undefined,
          //purchaseTime: undefined,
          maintenanceCycle: undefined,
          dataRange: undefined,
        },
        // 表单参数
        form: {},
        //列信息
        columns: [
          {key: 0, label: "序号", visible: true},
          {key: 1, label: "资产编号", visible: true},
          {key: 2, label: "资产IP", visible: true},
          {key: 3, label: "资产类型", visible: true},
          {key: 4, label: "资产端口", visible: true},
          {key: 5, label: "采购时间", visible: true},
          {key: 6, label: "维护周期", visible: true},
          {key: 5, label: "资产账号", visible: true},
          {key: 6, label: "资产名称", visible: true},
        ],
        queryForm: {
          assetsName: [{ pattern: /^[\u4E00-\u9FA5A-Za-z0-9]+$/, message: '不能为特殊字符', trigger: 'blur'}],
        },
        // 表单校验
        rules: {
          assetsId: [
            {required: true, message: '资产编号不能为空', trigger: 'blur'}
          ],
          assetsIp: [
            {required: true, message: '资产IP不能为空', trigger: 'blur'}
          ],
          assetsType: [
            {required: true, message: '资产类型不能为空', trigger: 'change'}
          ],
          assetsPort: [
            {required: true, message: '资产端口不能为空', trigger: 'blur'}
          ],
          assetsAccount: [
            {required: true, message: '资产账号不能为空', trigger: 'blur'}
          ],
          assetsPassword: [
            {required: true, message: '资产密码不能为空', trigger: 'blur'}
          ],
          assetsName: [
            {required: true, message: '资产名称不能为空', trigger: 'blur'}
          ],
          assetsStatus: [
            {required: true, message: '资产状态不能为空', trigger: 'change'}
          ],
          loading: null,
        }
      }
    },
    created() {
      this.getList()
      this.getDicts('t_assets_type').then(response => {
        this.typeOptions = response.data
      })
      this.getDicts('t_assets_status').then(response => {
        this.statusOptions = response.data
      })
      this.getDicts('t_maintenance_cycle').then(response => {
        this.cycleOptions = response.data
      })
    },
    methods: {
      /** 查询资产列表 */
      getList() {
        this.loading = true
        listAssets(this.addDateRange1(this.queryParams, this.dateRange)).then(response => {
          this.assetsList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 状态字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.assetsStatus)
      },
      //类型字典翻译
      typeFormat(row, column) {
        return this.selectDictLabel(this.typeOptions, row.assetsType)
      },
      cycleFormat(row, column) {
        return this.selectDictLabel(this.cycleOptions, row.maintenanceCycle)
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },

      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.$refs['queryForm'].validate(valid => {
          if (valid) {
            this.getList()
          }
        })
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams.assetsName = ''
        this.queryParams.assetsType = ''
        this.resetForm('queryForm')
        this.handleQuery()
      },
      reset() {
        this.form = {
          assetsId: "",
          assetsIp: "",
          assetsType: "",
          assetsPort: "",
          assetsAccount: "",
          assetsPassword: "",
          assetsName: "",
          purchaseTime: "",
          maintenanceCycle: "",
          assetsStatus: "",
          remark: "",
        }
        this.resetForm('form')
        this.dateRange = ''
      },

      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.assetsNum)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 查看按钮操作 */
      handleSelect(row) {
        this.reset()
        const assetsNum = row.assetsNum || this.assetsNum
        getAssets(assetsNum).then(response => {
          this.form = response.data
          this.look = true
          this.title = "查看资产信息"
        })
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加资产信息'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const assetsNum = row.assetsNum || this.assetsNum
        getAssets(assetsNum).then(response => {
          this.form = response.data
          this.open = true
          this.title = '修改资产信息'
        })
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.assetsNum != undefined) {
              updateAssets(this.form).then(response => {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addAssets(this.form).then(response => {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const assetsNums = row.assetsNum || this.ids
        this.$confirm('是否确认删除该数据项?', '警告', {
          cancelButtonClass: 'btn-custom-cancel',
          closeOnClickModal: false,
          type: 'warning'
        }).then(function () {
          return delAssets(assetsNums);
        }).then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        this.$confirm('是否确认导出所有资产信息数据项?', '提示', {
          cancelButtonClass: 'btn-custom-cancel',
          closeOnClickModal: false,
          type: 'info'
        }).then(function () {
          return exportAssets(queryParams)
        }).then(response => {
          this.download(response.msg)
        })
      },
      /** 链接远程设备信息*/
      handleRemoteDevice() {

        this.$confirm('链接时间可能稍长，是否继续执行？', '系统提示', {
          cancelButtonClass: 'btn-custom-cancel',
          closeOnClickModal: false,
          type: 'warning'
        }).then( ()=> {
          let loading = this.$loading({
            lock: true,
            text: "链接中....",
            background: "rgba(0, 0, 0, 0.7)",
          });
          queryAllMonitorInfos().then(Response => {
            loading.close()
            this.getList()
            this.msgSuccess('链接成功')
          })
        }).catch(function () {
        })
      }
    }
  }
</script>
<style scoped>
  .form_row {
    width: 250px;
  }
</style>
